package cn.edu.tju.cas.service.impl;


import cn.edu.tju.cas.dao.GroupDao;
import cn.edu.tju.cas.dao.UserDao;
import cn.edu.tju.cas.dao.UserGroupApplyDao;
import cn.edu.tju.cas.entity.GroupEntity;
import cn.edu.tju.cas.entity.UserEntity;
import cn.edu.tju.cas.entity.UserGroupApplyEntity;
import cn.edu.tju.cas.service.UserService;
import cn.edu.tju.cas.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by catold on 16/2/20.
 */
@Service("userService")
public class UserServiceImpl extends BaseServcieImpl<UserEntity, UserDao> implements UserService{
    @Autowired
    private UserDao userDao;

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private UserGroupApplyDao applyDao;

    public UserEntity findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    public UserGroupApplyEntity getCertificationInfo(int userId,int groupId){
        return applyDao.getByUserIdAndGroupId(userId, groupId);
    }

    @Override
    public boolean addGroupToUser(int userId, int groupId) {
        UserGroupApplyEntity userGroupApplyEntity = applyDao.getByUserIdAndGroupId(userId, groupId);

        boolean result = false;
        if(userGroupApplyEntity == null){
            userGroupApplyEntity = new UserGroupApplyEntity();
            userGroupApplyEntity.setStatus(1); // 已经审核通过
            userGroupApplyEntity.setGroupEntity(groupDao.findById(groupId));
            // 设置审核人
            userGroupApplyEntity.setCheckerName(SessionUtil.getUser().getUsername());
            // many-to-one userEntity
            userGroupApplyEntity.setUserEntity(userDao.findById(userId));
            result = applyDao.save(userGroupApplyEntity) > 0;
        }
        return result;
    }

    @Override
    public List<UserEntity> findByAdminType(int isAdmin) {
        return userDao.findByAdminType(isAdmin);
    }

//    @Override
//    public List<GroupEntity> findAllApplyRecords(int userId) {
//        List<Integer> groupIds = applyDao.getUserGroupIds(userId);
//        if(groupIds == null || groupIds.size() == 0){
//            return null;
//        }
//        return groupDao.findAllApplyRecords(groupIds);
//    }


    @Override
    public UserEntity getUserIdByIdCard(String idCard) {
        return userDao.finByIdCard(idCard);
    }

    @Resource(name = "userDao")
    public void setDao(UserDao userDao) {
        super.setDao(userDao);
    }


    @Override
    public UserEntity findById(int id){
        UserEntity userEntity = super.findById(id);
        if(userEntity == null){
            return null;
        }

        // 获取已通过申请的角色
        List<UserGroupApplyEntity> userApplys = applyDao.queryByUserAndState(id, 1);
        Set<GroupEntity> groupEntities = new HashSet<GroupEntity>();
        if(userApplys != null){
            for (UserGroupApplyEntity userGroupApplyEntity : userApplys) {
                if(userGroupApplyEntity.getStatus() == 1){
                    groupEntities.add(userGroupApplyEntity.getGroupEntity());
                }
            }

        }
        userEntity.setGroupEntities(groupEntities);
        return userEntity;
    }


    @Override
    public List<UserEntity> findAll(){
        List<UserEntity> userEntities = new ArrayList<>();
        List<Integer> userIds = userDao.findUserIds();
        for(int id:userIds ){
            userEntities.add(findById(id));
        }
        return userEntities;
//        return userDao.findAll();
    }
}
